import * as actions from '../actions/index';

// Whether to show Merge Button, because there are most systems not need to show this button, so this place use === condition
function isNeededToDoMerge(result) {
  return result.is_support_merge;
}

function processCrDetail(result) {
  // Whether to show the merge button
  result.isNeededToDoMerge = isNeededToDoMerge(result);
  // Whether to show some modify buttons
  // If this value is undefined, then regard it as true.
  result.canModify = result.is_support_modify === undefined ? true : result.is_support_modify;
  return result;
}

function getSingleData(arr) {
  const newArr = [];
  arr.forEach((item) => {
    const hasSame = newArr.some((it) => {
      return it.build_issue_id === item.build_issue_id;
    });
    if (!hasSame) {
      newArr.push(item);
    }
  });
  return newArr;
}

// 页面初始化数据
function getInitialState(pageInfo) {
  const { codeReviewDetail } = pageInfo;

  const initialState = {
    codeReviewDetail,
    summaryLogs: null,
    sourceBranchExist: codeReviewDetail.sourceBranchExist, // 源分支是否存在
    targetBranchExist: codeReviewDetail.targetBranchExist, // 目标分支是否存在
    approve_status: {},
    isLoadingDetail: true,
    commitHistory: null,
    selectedKeys: '',
    title: '',
    currentPage: 1,
    isLoading: true,
    logTotal: 0,
    mergeRequestType: [],
    mergeRequestBuilds: [],
    allIssues: [],
    issueDetailList: [],
    changesCount: 0,
    mrTabKey: '',
    commitHistoryLoaded: false,
    isMerging: false,
    targetBranchSetting: {},
    changeRequest: [],
    mrCommentConfig: {
      limit_comment: false,
    },
    project_clone_download: {},
  };

  return initialState;
}

const defaultAction = {
  type: 'doNothing',
};

export default function index(state = getInitialState(window.FORCE), action = defaultAction) {
  switch (action.type) {
    case actions.RECEIVE_CRDETAIL: {
      const data = {
        ...state,
        codeReviewDetail: processCrDetail(action.data),
        isLoadingDetail: false,
      };

      // codeOwnerFocusMode 默认为空，如果 code_owner_mode 为 FOCUS，则将其设为 own
      if (!state.codeOwnerFocusMode && action.data.code_owner_mode === 'FOCUS') {
        data.codeOwnerFocusMode = 'own';
      }
      return data;
    }
    case actions.RECEIVE_APPROVE_STATUS:
      return {
        ...state,
        approve_status: action.data,
      };
    case actions.RECEIVE_COMMIT_HISTORY:
      return {
        ...state,
        commitHistory: action.data,
        commitHistoryLoaded: true,
      };
    case actions.RECEIVE_SUMMARY_LOGS:
      return {
        ...state,
        summaryLogs: action.data.list,
        logTotal: action.data.amount,
        currentPage: action.data.currentPage,
        isLoading: false,
      };
    case actions.RECEIVE_MERGE_DATA:
      return {
        ...state,
        mergeRequestType: action.data.merge_types,
        is_enable_reviewer_recommend: action.data.is_enable_reviewer_recommend,
      };
    case actions.RECEIVE_MERGE_BUILDS:
      return {
        ...state,
        mergeRequestBuilds: action.data,
      };
    case actions.RECEIVE_MERGE_ISSUES:
      return {
        ...state,
        allIssues: action.data,
      };
    case actions.RECEIVE_MERGE_ISSUE_DETAILS:
      return {
        ...state,
        issueDetailList: getSingleData([...state.issueDetailList, ...action.data]),
      };
    case actions.RECEIVE_TITLE:
      return {
        ...state,
        title: action.data,
      };
    case actions.DELETE_SOURCE_BRANCH:
      return {
        ...state,
        sourceBranchExist: 'false',
      };
    case actions.CHANGE_MR_TAB_KEY:
      return {
        ...state,
        mrTabKey: action.data,
      };
    case actions.UPDATE_PAGE_INFO:
      return getInitialState(action.data);
    case actions.UPDATE_DATA:
      return { ...state, ...action.data };
    case actions.RECEIVE_TARGET_BRANCH_SETTING:
      return {
        ...state,
        targetBranchSetting: action.data,
      };
    case actions.RECEIVE_CHANGE_REQUEST:
      return {
        ...state,
        changeRequest: action.data,
      };
    case actions.SET_PROJECT_CLONE_DOWNLOAD:
      return { ...state, project_clone_download: action.data };
    default:
      return state;
  }
}
